package com.fdb.efp.nls.api.rest;

import com.fdb.efp.nls.api.service.NlsBizCodeReverseNameService;
import com.fdb.basic.framework.core.constant.DataRuleType;
import com.fdb.basic.framework.core.constant.PubConstant;
import com.fdb.basic.framework.core.constant.ResponseData;
import com.fdb.basic.framework.web.controller.AbstractController;
import com.fdb.efp.e4a.service.facade.SRoleDataRuleService;
import com.fdb.efp.limit.service.facade.LmtPrdContService;
import com.fdb.efp.limit.service.vo.LmtPrdContVO;
import com.fdb.efp.nls.service.facade.sed.SedNlsCreditInfoService;
import com.fdb.efp.nls.service.facade.sx.NlsCreditInfoService;
import com.fdb.efp.nls.service.vo.NlsApplyInfoVO;
import com.fdb.efp.nls.service.vo.sx.NlsCreditInfoVO;
import com.fdb.efp.nls.service.vo.sx.SedNlsCreditInfo;
import com.fdb.efp.nls.service.vo.sx.SedNlsCreditLmtCtrInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;


/**
 * 
  * TODO
  * @author wanglihua
  * @since 2019年3月13日 下午7:39:00
  * @version 1.0
 */

@RestController
@RequestMapping("/api")
public class QueryNlsCreditInfoController extends AbstractController {
	
	//@Autowired
	//@Qualifier("sedNlsCreditInfoService")
	//private SedNlsCreditInfoService sedNlsCreditInfoService;

	@Autowired
	@Qualifier("nlsCreditInfoService")
	private NlsCreditInfoService nlsCreditInfoService;
	
	@Autowired
	@Qualifier("lmtPrdContService")
	private LmtPrdContService lmtPrdContService;

	@Autowired
	@Qualifier("sRoleDataRuleService")
	private SRoleDataRuleService sRoleDataRuleService;

	@Autowired
	private NlsBizCodeReverseNameService nlsBizCodeReverseNameService;

	/**
	 * 授信申请查询: <br>
	 *
	 * 0.1:caoxin:2019/5/31 11:01: TODO: <br>
	 * @author caoxin
	 * @return
	 * @since 2019/5/31 11:01
	 * @date 2019/5/31 11:01
	 * @version 0.1
	 */
	@PostMapping(value = "/")
	public @ResponseBody
    ResponseData<List<SedNlsCreditInfo>> queryAllByPage(@RequestBody NlsCreditInfoVO vo) {
        List<SedNlsCreditInfo> creditInfos = null;//sedNlsCreditInfoService.getCreditInfoService(vo);
		return getResponseData(creditInfos);
	}
	/**
	 * 放款申请查询: <br>
	 * 0.1:caoxin:2019/5/31 11:01: <br>
	 * @author caoxin
	 * @return
	 * @since 2019/5/31 11:01
	 * @date 2019/5/31 11:01
	 * @version 0.1
	 */
	@PostMapping(value = "/nls/sedapply/infos")
	public @ResponseBody
	ResponseData<List<SedNlsCreditLmtCtrInfo>> queryNlsCreditLmtCtrInfoByPage(@RequestBody NlsApplyInfoVO vo) throws Exception {
		vo.setPrdCodebyBaseInfo("11110001");
        List<SedNlsCreditLmtCtrInfo> outputs = null;//sedNlsCreditInfoService.getApplyInfoLmtCtrService(vo);
		return getResponseData(outputs);
	}

	/**
	 *
	 * TODO <br>
	 * @author wanglihua
	 * @since 2019年6月6日 下午5:11:10
	 * @date 2019年6月6日 下午5:11:10
	 * @version 0.1
	 */
	@PostMapping(value = "/nls/credit/infos")
	public @ResponseBody ResponseData<List<NlsCreditInfoVO>> queryAllInfosByPage(@RequestBody NlsCreditInfoVO vo) {
		List<NlsCreditInfoVO> outputVo = null;
		setUserInfoToVO(vo);
		// 根据用户角色获取用户权限方案
		Map<String,String> dataRuleTypeMap = sRoleDataRuleService.getDataRuleType(httpRequest.getRequestURI(), getUserRoles());
		vo.setPrdCodebyBaseInfo(dataRuleTypeMap == null ? null:dataRuleTypeMap.get(PubConstant.PRD_CODE));
		DataRuleType dataRuleType = DataRuleType.getDataRuleType(dataRuleTypeMap == null ? null:dataRuleTypeMap.get(PubConstant.PERMIS_TYPE));
		setUserInfoToVO(vo);
		// 根据权限方案调用不同的方案进行数据查询
		switch (dataRuleType) {
		case Owner:
			outputVo = nlsCreditInfoService.queryAllOwner(vo);
			break;
		case CurrOrg:
			outputVo = nlsCreditInfoService.queryAllCurrOrg(vo);
			break;
		case CurrDownOrg:
			outputVo = nlsCreditInfoService.queryAllCurrDownOrg(vo);
			break;
		case CurrOwnerPrd:
			outputVo = nlsCreditInfoService.queryAllCurrOwnerPrd(vo);
			break;
		default:
			outputVo = nlsCreditInfoService.queryAllOwner(vo);
			break;
		}
		if(Objects.nonNull(outputVo)) {
			addEleToOutputVo(outputVo);
		}
		nlsBizCodeReverseNameService.displayOrgName(outputVo,"nlsOperOrgid","nlsOperOrgName");
		return getResponseData(outputVo);
	}
	/**
	 *
	 * TODO 查询授信协议表得到所需数据<br>
	 * @author wanglihua
	 * @since 2019年7月18日 下午8:18:14
	 * @date 2019年7月18日 下午8:18:14
	 * @version 0.1
	 */
	private void addEleToOutputVo(List<NlsCreditInfoVO> outputVo) {
		for (NlsCreditInfoVO item : outputVo) {
			LmtPrdContVO rs = Optional.ofNullable(lmtPrdContService.qryLmtPrdContByLmtApplySeq(item.getLmtApplySeq())).orElseGet(LmtPrdContVO::new);
			item.setLmtContNo(rs.getLmtContNo());
			item.setStartDate(rs.getStartDate());
			item.setExpireDate(rs.getExpireDate());
			item.setMainBrId(rs.getMainBrId());
			item.setChargeoffBrId(rs.getChargeoffBrId());
		}
	}

	@PostMapping(value = "/nls/credit/infos/by/cust")
	public @ResponseBody ResponseData<List<NlsCreditInfoVO>> queryAllInfoByCustByPage(@RequestBody NlsCreditInfoVO vo) {
		List<NlsCreditInfoVO> outputVo = nlsCreditInfoService.queryAllByConditionPage(vo);
		nlsBizCodeReverseNameService.displayUserName(outputVo,"cusManager","cusManagerName");
		nlsBizCodeReverseNameService.displayOrgName(outputVo,"nlsOperOrgid","nlsOperOrgName");
		return getResponseData(outputVo);
	}
}
